package com.example.demo.service.impl;

import com.example.demo.entity.ArticleLike;
import com.example.demo.entity.VO.ArticleLikeVO;
import com.example.demo.mapper.ArticleLikeMapper;
import com.example.demo.service.IArticleLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文章点赞表 服务实现类
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-05
 */
@Service
public class ArticleLikeServiceImpl extends ServiceImpl<ArticleLikeMapper, ArticleLike> implements IArticleLikeService {

    @Autowired
    private ArticleLikeMapper articleLikeMapper;

    @Override
    @Transactional
    public Integer addLike(ArticleLike articleLike) {
        if (articleLike.getUserId() < 0) {
            // 这里我选择了返回一个错误码 -1
            return -1;
        }
        List<ArticleLike> articleLikes = articleLikeMapper.selectLikesByUserAndArticle(articleLike);
        if (articleLikes == null || articleLikes.size() < 1){
            articleLikeMapper.insertLike(articleLike);
            return 1;
        }
        return 0;
    }

    @Override
    public Integer countLikesByArticle(Integer articleId) {
        return articleLikeMapper.countLikesByArticle(articleId);
    }

    @Override
    public List<ArticleLike> selectLikesByUserAndArticle(ArticleLike articleLike) {

       return articleLikeMapper.selectLikesByUserAndArticle(articleLike);
    }

    @Override
    public List<ArticleLike> selectAllLikes() {
        return articleLikeMapper.selectAllLikes();
    }

    @Override
    public Integer deleteLike(ArticleLike articleLike) {
        if (articleLikeMapper.selectLikesByUserAndArticle(articleLike)!=null){
            articleLikeMapper.cancelLike(articleLike);
            return 1;
        }
        return 0;
    }
}
